﻿var equipmentTypeListTableVue = new Vue({
    el: "#equipmentTypeListTable",
    data: {
        typeList: []
    },
    methods: {
        AddType: function () {
            AddEquipmentType(this);
        },
        EditType: function (type) {
            EditEquipmentType(this, type);
        },
        DeleteType: function (type) {
            DeleteEquipmentType(this, type);
        }

    },
    created: function () {
        //页面加载前初始的数据
        InitialAztechScript();
    },
    mounted: function () {
        //页面加载后初始的数据
        ListEquipmentType(this);
    }
});

/**
 * 列出所有设备类型
 */
function ListEquipmentType(vue) {
    ShowAzLoading();

    $.ajax({
        type: "GET",
        url: "/Equipment/ListEquipmentType?v=" + Math.random(),
        data: { },
        success: function (res) {
            if (res.status == "Success") {
                vue.typeList = res.data;
            } else {
                toastr.error(res.message);
            }
            HideAzLoading();
        },
        error: function (err) {
            console.log(err);
            HideAzLoading();
            toastr.error("加载数据异常，请稍后再试或联系管理员Abnormal data loading. Please try again later or contact the administrator。");
        }
    });
}

/**
 * 新增设备类型
 */
function AddEquipmentType(vue) {
    OpenAzModal("新增设备类型New equipment type", "/Equipment/Page/TypeDetail?IsNew=true", function () {
        ListEquipmentType(vue);
    });
}

/**
 * 编辑设备类型
 */
function EditEquipmentType(vue, type) {
    OpenAzModal("编辑设备类型Edit device type", "/Equipment/Page/TypeDetail?TypeID=" + type.ID, function () {
        ListEquipmentType(vue);
    });
}

/**
 * 删除设备类型
 */
function DeleteEquipmentType(vue, type) {
    var message = "确认删除设备类型Confirm deletion of device type【" + type.Name + "】?";
    if (confirm(message)) {
        ShowAzLoading();
        $.ajax({
            type: "POST",
            url: "/Equipment/DeleteEquipmentType",
            data: { TypeID: type.ID },
            success: function (res) {
                if (res.status == "Success") {
                    toastr.success("删除设备类型信息成功Successfully deleted device type information。");
                    ListEquipmentType(vue);
                } else {
                    toastr.error(res.message);
                }
                HideAzLoading();
            },
            error: function (err) {
                console.log(err);
                toastr.error("服务器发生错误，请稍候再试An error occurred in the server. Please try again later。");
                HideAzLoading();
            }
        })
    }
}